package com.jetgege.smartbox.BoxSetting;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.jetgege.smartbox.Base.App;
import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 管理员页面
 */
public class MBoxSettingActivity extends BaseActivity {

    @BindView(R.id.boxset_toolbar)
    Toolbar boxsetToolbar;
    @BindView(R.id.lv_UserList)
    ListView lvUserList;

    UserAdapter userAdapter;
    @BindView(R.id.bt_Delete)
    Button btDelete;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_box_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
        eventHandle();
    }

    private void initValue() {
        boxsetToolbar.setTitle("设置");
        setSupportActionBar(boxsetToolbar);
        // Navigation Icon 要設定在 setSupoortActionBar 才有作用
        // 否則會出現 back button
        boxsetToolbar.setNavigationIcon(R.drawable.bar_back_btn);

        userAdapter = new UserAdapter(App.mDevices.getUserModels());
        lvUserList.setAdapter(userAdapter);
    }

    private void eventHandle() {
        boxsetToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });

        lvUserList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent =new Intent(MBoxSettingActivity.this,FingerprintActivity.class);
                intent.putExtra("Position",position);
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
    }

    //  添加编辑
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mbox_setting, menu);
        return true;
    }

    /**
     * 设置编辑状态
     *默认不能操作管理员自己
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getTitle().equals("编辑")) {
            item.setTitle("取消");
            for (int i = 0; i < userAdapter.checkBoxes.size(); i++) {
                if (i!=0) userAdapter.checkBoxes.get(i).setVisibility(View.VISIBLE);
                userAdapter.imageViews.get(i).setVisibility(View.GONE);
                btDelete.setVisibility(View.VISIBLE);
                setAnimation(btDelete, true);
            }
        } else {
            item.setTitle("编辑");
            for (int i = 0; i < userAdapter.checkBoxes.size(); i++) {
                userAdapter.imageViews.get(i).setVisibility(View.VISIBLE);
                userAdapter.checkBoxes.get(i).setVisibility(View.GONE);
                setAnimation(btDelete, false);
            }
        }
        //startActivity(new Intent(ForgetPWDActivity.this,NewPWDActivity.class));

        return true;
    }

    ScaleAnimation scaleAnimation;

    private void setAnimation(View v, final boolean isShow) {
        if (isShow)
            scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        else
            scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);//动画时间
        scaleAnimation.setFillAfter(true);//动画结束后停留在当前位置
        v.startAnimation(scaleAnimation);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束后控件设为不可见
                if (!isShow) btDelete.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @OnClick(R.id.bt_Delete)
    public void onViewClicked() {

       //默认管理员不能删除自己，只能恢复出厂设置重置管理员（重置设备）
       for (int i=userAdapter.checkBoxes.size()-1;i>0 ;i--){
           if(userAdapter.checkBoxes.get(i).isChecked())
               App.mDevices.getUserModels().remove(i);
       }
       userAdapter.notifyDataSetChanged();
    }
}
